package com.itheima.utils;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.util.ArrayList;
import java.util.Set;

/**
 * @version V1.0
 * @author: ljx
 * @date: 2020/01/13 16:58
 * @description:
 */
public class QiNiuUtil {
    private static String accessKey = "2Xr6m0VKCA-f_nfIYLHLBCIXLcWjRabtriSYGPhK";
    private static String secretKey = "Y5fvPG1yn5pIJyud0qAJiO1Fp252A8M7UByQ0vKy";
    private static String buket = "health-ljx";

    /**
     * @param bytes    字节数组
     * @param fileName 上传到七牛服务器后文件的名字，不可重复，否则上传失败
     * @throws QiniuException
     */
    public static void uploadFile(byte[] bytes, String fileName) throws QiniuException {
        Configuration cfg = new Configuration(Region.huadong());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String token = auth.uploadToken(buket);
        Response response = uploadManager.put(bytes, fileName, token);
        DefaultPutRet putRet = JSONObject.parseObject(response.bodyString(), DefaultPutRet.class);
        System.out.println(putRet.hash);
        System.out.println(putRet.key);
    }

    /**
     * 七牛服务器空间中文件的名字
     *
     * @param imgName
     */
    public static void delImg(String imgName) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huadong());
        //...其他参数参考类注释
        Auth auth = Auth.create(accessKey, secretKey);
        //创建空间管理对象，传入上传策略对象和配置对象
        BucketManager bucketManager = new BucketManager(auth, cfg);

        try {
            bucketManager.delete(buket, imgName);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }

    public static void delBatch(Set<String> set) {
        String[] strings = set.toArray(new String[]{});
        ArrayList<String[]> list = new ArrayList<>();

        for (int j = 0; j < Math.ceil(strings.length * 1.0 / 1000); j++) {
            String[] childStrings = new String[1000];
            list.add(childStrings);
        }
        for (String[] strings1 : list) {

            for (int i = 0; i < strings.length; i++) {
                strings1[i] = strings[i];
            }
        }

        try {
            Configuration cfg = new Configuration(Region.huadong());
            Auth auth = Auth.create(accessKey, secretKey);
            BucketManager bucketManager = new BucketManager(auth, cfg);

            BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
            // batchOperations.addDeleteOp(buket,);
            Response response = bucketManager.batch(batchOperations);
            BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
        } catch (QiniuException e) {
            e.printStackTrace();
        }

      /*  for (int i = 0; i < keyList.length; i++) {
            BatchStatus status = batchStatusList[i];
            String key = keyList[i];
            System.out.print(key + "\t");
            if (status.code == 200) {
                System.out.println("delete success");
            } else {
                System.out.println(status.data.error);
            }
        }*/
    }


}
